# -*- coding: utf-8 -*-
"""
@File    : compare_camera.py
@Author  : LY
@Time    : 2021/5/12 19:52
"""
from FFT_Interpolation import line_cal, line_cal_fix, FFT_cal, FFT_interpolation_2
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy import signal
from tqdm import tqdm

Lamda = 632.8e-9
pix_size = 5.3e-6
pix_num = 1280
window_gaussian = signal.gaussian(pix_num, std=pix_num / 4)
result_set_1 = []
result_set_2 = []
result_set_3 = []
''' 
    Reading from Video file
'''
img_set = []
file_name = r'C:\Users\admin\Desktop\Resolution\Square_hor\3__0.1urad_2Hz\3.avi'
cap = cv2.VideoCapture(file_name)
frame_num = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(frame_num, "Frames")
print('ok')
for k in range(frame_num):
    ret, frame = cap.read()
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img_set.append(img)

''' 
    For single frame
'''
img = img_set[0]
hor_freqs = []
window = signal.gaussian(1200, std=1200 / 8)
for line in img[262:762]:
    line = [i - 6 for i in line]
    line = line[:1200] * window

    freq_estim_2, phase_estim_2, freqline, sig_magnitude, sig_phase, m_k_num, X_m_k, freq_for_phase = FFT_interpolation_2(
        sig=line, tau0=pix_size, zero_num=1000 * 99, DC_num=800)
    print(freq_estim_2)
    hor_freqs.append(freq_estim_2)

hor_freqs = [Lamda*f/2*1e6 for f in hor_freqs]
plt.plot(hor_freqs, '.-')
plt.show()

''' 
    For Frame Set
'''
# for i in tqdm(range(len(img_set))):
#     temp_1 = []
#     temp_2 = []
#     temp_3 = []
#     for j in range(100):
#         line = img_set[i][512-50+j]
#         freq_estim_2, phase_estim_2, freqline, sig_magnitude, sig_phase, m_k_num, X_m_k, freq_for_phase = FFT_interpolation_2(
#                 sig=line, tau0=pix_size, zero_num=pix_num * 99, DC_num=800)
#         temp_1.append(freq_estim_2)
#         line = line - np.average(line[1000:])
#         freq_estim_2, phase_estim_2, freqline, sig_magnitude, sig_phase, m_k_num, X_m_k, freq_for_phase = FFT_interpolation_2(
#                 sig=line*signal.gaussian(pix_num, std=pix_num/4), tau0=pix_size, zero_num=pix_num * 99, DC_num=800)
#         temp_2.append(freq_estim_2)
#         freq_estim_2, phase_estim_2, freqline, sig_magnitude, sig_phase, m_k_num, X_m_k, freq_for_phase = FFT_interpolation_2(
#             sig=line * signal.gaussian(pix_num, std=pix_num/8), tau0=pix_size, zero_num=pix_num * 99, DC_num=800)
#         temp_3.append(freq_estim_2)
#     result_set_1.append(np.mean(temp_1))
#     result_set_2.append(np.mean(temp_2))
#     result_set_3.append(np.mean(temp_3))
#
# result_set_1 = [Lamda*f/2*1e6 for f in result_set_1]
# result_set_2 = [Lamda*f/2*1e6 for f in result_set_2]
# result_set_3 = [Lamda*f/2*1e6 for f in result_set_3]
#
# print(np.std(result_set_1))
# print(np.std(result_set_2))
# print(np.std(result_set_3))
#
# # plt.figure(1)
# # plt.plot(line, 'k')
# # plt.plot(line*signal.gaussian(pix_num, std=pix_num/4), 'b')
# # plt.plot(line*signal.gaussian(pix_num, std=pix_num/8), 'r')
#
# plt.figure(2)
# plt.plot(result_set_1, 'k')
# plt.plot(result_set_2, 'b')
# plt.plot(result_set_3, 'r')
#
# plt.show()
